package com.yyge.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.ibatis.datasource.DataSourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.util.Properties;

public class DataSourceFactoryHandler implements DataSourceFactory {
   private static final Logger LOGGER = LoggerFactory.getLogger(DataSourceFactoryHandler.class);

    private Properties properties;
    /**
     * mybatis全局配置文件中加载的properties
     * @param props
     */
    @Override
    public void setProperties(Properties props) {
        this.properties = props;
        LOGGER.debug("接收到mybatis全局配置文件传递数据源环境参数配置{}", props);
    }

    /**
     * 自定义连接池
     * @return
     */
    @Override
    public DataSource getDataSource() {
        DataSource dataSource = null;
        try {
            dataSource = DruidDataSourceFactory.createDataSource(this.properties);
            LOGGER.debug("创建druid连接池成功");
        } catch (Exception e) {
            // e.printStackTrace();
            LOGGER.error(e.getMessage());
        }
        return dataSource;
    }
}
